package fun.tan90.http.plugin.config;

import io.vertx.core.Vertx;
import lombok.extern.slf4j.Slf4j;
import org.noear.snack.ONode;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.core.AppContext;
import org.noear.solon.core.bean.LifecycleBean;

@Slf4j
@Component
public class VertxHttpServerFactory implements LifecycleBean {

    @Inject
    AppContext context;

    String data = """
            [{"port":9091,"path":"/data"},{"port":9092,"path":"/data"},{"port":9093,"path":"/data"}]
            """;

    @Override
    public void start() {
        log.info("VertxHttpServerFactory start");
        Vertx vertx = Vertx.vertx();
        context.wrapAndPut(Vertx.class, vertx);

        ONode root = ONode.load(data);
        root.ary().forEach(node -> {
            int port = node.get("port").getInt();
            String path = node.get("path").getString();
            vertx.deployVerticle(new VertxHttpServer(port, path, 1024 * 1024 * 10), res -> {
                if (res.succeeded()) {
                    log.info("HTTP 服务器运行在: http://localhost:{}", port);
                } else {
                    log.error("服务启动失败: {}", res.cause().getMessage());
                }
            });
        });
    }
}
